import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:yyads_text/global/config.dart';
import 'package:yyads_text/yyads_text.dart';

class BannerView extends StatefulWidget {
  final BannerCallBack? bannerCallBack;
  final double width;
  final double height;
  const BannerView({ Key? key,required this.bannerCallBack,required this.height,required this.width }) : super(key: key);

  @override
  _BannerViewState createState() => _BannerViewState();
}

class _BannerViewState extends State<BannerView> {
  Widget? child;
  MethodChannel? channel;
  @override
  Widget build(BuildContext context) {
    if(defaultTargetPlatform == TargetPlatform.android){
      return Container(
        width: double.infinity,
        child:AndroidView(
          viewType: Config.bannerPage,
          creationParams: {
            "width":widget.width.toString(),
            "height":widget.height.toString(),
          },
          onPlatformViewCreated: (int id){
            channel = MethodChannel("${Config.bannerPage}"+"_"+"$id");
            channel?.setMethodCallHandler((call) async{
              switch (call.method) {
                case "onError":
                if(widget.bannerCallBack != null){
                  widget.bannerCallBack?.error!(call.arguments);
                }
                  
                  break;
                case "onLoaded":
                if(widget.bannerCallBack != null){
                  widget.bannerCallBack?.loaded!();
                }
                
                  break;
                case "onShow":
                if(widget.bannerCallBack != null){
                  widget.bannerCallBack?.adShow!();
                }
                
                  break;
                case "onClosed":
                if(widget.bannerCallBack != null){
                  widget.bannerCallBack?.closed!();
                }
                
                  break;
                case "onClicked":
                if(widget.bannerCallBack != null){
                  widget.bannerCallBack?.adClicked!();
                }
                
                  break;
                case "onShowAdOverLay":
                if(widget.bannerCallBack != null){
                  widget.bannerCallBack?.showAdOverLay!();
                }
                
                  break;
                case "onAdCloseOverLay":
                if(widget.bannerCallBack != null){
                  widget.bannerCallBack?.adCloseOverLay!();
                }
                
                  break;
                case "onDislikeShow":
                if(widget.bannerCallBack != null){
                  widget.bannerCallBack?.dislikeShow!();
                }
                
                  break;
                case "onDislikeSelected":
                if(widget.bannerCallBack != null){
                  widget.bannerCallBack?.dislikeSelected!(call.arguments);
                }
                
                  break;
                case "onDislikeCanceled":
                if(widget.bannerCallBack != null){
                  widget.bannerCallBack?.dislikeCanceled!();
                }
                
                  break;
                default:
              }
            });
          },
           creationParamsCodec: const StandardMessageCodec(),
          ));
    }else{
      return Container();
    }
  }
}